import { getMSymbol } from '@/api/mSymbol/mSymbol'
const mSymbol = {
  state: {
    // 设置自定义符号
    MoneySymbol: {
      name: '￥',
      startTime: 0
    }
  },
  mutations: {
    SET_MONEY_SYMBOL: (state, mSymbol) => {
      state.MoneySymbol = mSymbol
    }
  },
  actions: {
    setMoneySymbol({ commit }, mSymbol) {
      let mSymbolObj = window.localStorage.getItem('mSymbol') || ''
      if (mSymbolObj) {
        mSymbolObj = JSON.parse(mSymbolObj)
        const newDate = new Date().getTime()
        if (newDate - mSymbolObj.startTime < 1000 * 60) {
          // console.log('时间小于设定时间')
          commit('SET_MONEY_SYMBOL', mSymbolObj)
        } else {
          getMSymbol().then(res => {
            if (res.data.is_use_price_symbol === '1' && res.data.price_symbol_value != '') {
              // console.log('重新调用')
              const symbolObj = {
                name: res.data.price_symbol_value,
                startTime: new Date().getTime()
              }
              window.localStorage.setItem('mSymbol', JSON.stringify(symbolObj))
              commit('SET_MONEY_SYMBOL', symbolObj)
            }
          })
        }
      } else {
        getMSymbol().then(res => {
          if (res.data.is_use_price_symbol === '1' && res.data.price_symbol_value != '') {
            // console.log('没有就调用')
            const symbolObj = {
              name: res.data.price_symbol_value,
              startTime: new Date().getTime()
            }
            window.localStorage.setItem('mSymbol', JSON.stringify(symbolObj))
            commit('SET_MONEY_SYMBOL', symbolObj)
          }
        })
      }
    }
  }
}

export default mSymbol
